/**
*	Group: Y1
*	The KeyInput class manages input from the keyboard on the various panels
*	@author Mason Kwiat
*/

package lightspeed.Media.Graphics;
import java.awt.event.*;
import javax.swing.*;

public class KeyInput extends KeyAdapter
{
	/**
	*Initializes WindowManager
	**/
	WindowManager wm = null;
	
	/**
	*	Constructor: creates an instance of MouseInput
	*	@param wm WindowManager - so that WindowManager can handle MouseInput events directly
	*/
	public KeyInput(WindowManager wm)
	{
		this.wm = wm;
	}
	
	/**
	* Lets the system know what to do when a key is being typed
	* Allows the user to toggle between the three panels by using the keys '1', '2', and '3.'
	* @param e KeyEvent to let the system know when the keyboard is doing something
	**/
	public void keyTyped(KeyEvent e)
	{
		int keyCode = e.getKeyChar();
		
		if(keyCode == KeyEvent.VK_1)
			wm.setTopLayer(WindowManager.MENU_PANEL);
		
		if(keyCode == KeyEvent.VK_2)
			wm.setTopLayer(WindowManager.LOBBY_PANEL);
		
		if(keyCode == KeyEvent.VK_3)
			wm.setTopLayer(WindowManager.GAME_PANEL);
	}
	
	/**
	* Lets the system know what to do when a key is pressed
	* Allows the user to maneuver in the game. Sets booleans to 'true' so that avatar moves in the direction specified
	* @param e KeyEvent to let the system know when the keyboard is doing something
	**/
	public void keyPressed(KeyEvent e)
	{
		int keyCode = e.getKeyCode();
		if(WindowManager.currentPanel == WindowManager.GAME_PANEL && wm.client != null)
		{
			if (keyCode == KeyEvent.VK_LEFT)
				WindowManager.client.cpacket.leftDown = true;
			
			if(keyCode == KeyEvent.VK_RIGHT)
				WindowManager.client.cpacket.rightDown = true;
			
			if(keyCode == KeyEvent.VK_UP)
				WindowManager.client.cpacket.upDown = true;
			
			if(keyCode == KeyEvent.VK_SPACE)
			{
				WindowManager.client.cpacket.spaceDown = true;
			}
			if(WindowManager.gamePanel.restart == 1 && keyCode == KeyEvent.VK_ENTER)
			{
				System.out.println("restarting");
				WindowManager.gamePanel.restart = 0;
				WindowManager.gamePanel.startGraphics();
			}
		}
	}
	
	/**
	* Lets the system know what to do when a key is released
	* Allows the user to maneuver in the game. Sets booleans to 'false' so that the avatar stops moving
	* @param e KeyEvent to let the system know when the keyboard is doing something
	**/
	public void keyReleased(KeyEvent e)
	{
		int keyCode = e.getKeyCode();
		if(WindowManager.currentPanel == WindowManager.GAME_PANEL && wm.client != null)
		{
			if (keyCode == KeyEvent.VK_LEFT)
				WindowManager.client.cpacket.leftDown = false;
			
			if(keyCode == KeyEvent.VK_RIGHT)
				WindowManager.client.cpacket.rightDown = false;
			
			if(keyCode == KeyEvent.VK_UP)
				WindowManager.client.cpacket.upDown = false;
			
			if(keyCode == KeyEvent.VK_SPACE)
				WindowManager.client.cpacket.spaceDown = false;
		}
	}
}